﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class SymbolShow
    {
        public int Add(Chain.Model.SymbolShow model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into SymbolShow(");
            builder.Append("SymbolTitle,SymbolPhoto,SymbolDesc,SymbolTime)");
            builder.Append(" values (");
            builder.Append("@SymbolTitle,@SymbolPhoto,@SymbolDesc,@SymbolTime)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SymbolTitle", SqlDbType.NVarChar, 50), new SqlParameter("@SymbolPhoto", SqlDbType.NVarChar, 200), new SqlParameter("@SymbolDesc", SqlDbType.NVarChar, 0xfa0), new SqlParameter("@SymbolTime", SqlDbType.DateTime) };
            cmdParms[0].Value = model.SymbolTitle;
            cmdParms[1].Value = model.SymbolPhoto;
            cmdParms[2].Value = model.SymbolDesc;
            cmdParms[3].Value = model.SymbolTime;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int SymbolID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SymbolShow ");
            builder.Append(" where SymbolID=@SymbolID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SymbolID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SymbolID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string SymbolIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from SymbolShow ");
            builder.Append(" where SymbolID in (" + SymbolIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int SymbolID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from SymbolShow");
            builder.Append(" where SymbolID=@SymbolID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SymbolID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SymbolID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SymbolID,SymbolTitle,SymbolPhoto,SymbolDesc,SymbolTime ");
            builder.Append(" FROM SymbolShow ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" SymbolID,SymbolTitle,SymbolPhoto,SymbolDesc,SymbolTime ");
            builder.Append(" FROM SymbolShow ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.SymbolID desc");
            }
            builder.Append(")AS Row, T.*  from SymbolShow T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.SymbolShow GetModel(int SymbolID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 SymbolID,SymbolTitle,SymbolPhoto,SymbolDesc,SymbolTime from SymbolShow ");
            builder.Append(" where SymbolID=@SymbolID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SymbolID", SqlDbType.Int, 4) };
            cmdParms[0].Value = SymbolID;
            Chain.Model.SymbolShow show = new Chain.Model.SymbolShow();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["SymbolID"] != null) && (set.Tables[0].Rows[0]["SymbolID"].ToString() != ""))
            {
                show.SymbolID = int.Parse(set.Tables[0].Rows[0]["SymbolID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["SymbolTitle"] != null) && (set.Tables[0].Rows[0]["SymbolTitle"].ToString() != ""))
            {
                show.SymbolTitle = set.Tables[0].Rows[0]["SymbolTitle"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SymbolPhoto"] != null) && (set.Tables[0].Rows[0]["SymbolPhoto"].ToString() != ""))
            {
                show.SymbolPhoto = set.Tables[0].Rows[0]["SymbolPhoto"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SymbolDesc"] != null) && (set.Tables[0].Rows[0]["SymbolDesc"].ToString() != ""))
            {
                show.SymbolDesc = set.Tables[0].Rows[0]["SymbolDesc"].ToString();
            }
            if ((set.Tables[0].Rows[0]["SymbolTime"] != null) && (set.Tables[0].Rows[0]["SymbolTime"].ToString() != ""))
            {
                show.SymbolTime = DateTime.Parse(set.Tables[0].Rows[0]["SymbolTime"].ToString());
            }
            return show;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM SymbolShow ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetSymbolShowInfo(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "SymbolShow";
            string[] column = new string[] { "SymbolID,SymbolTitle,SymbolPhoto,SymbolDesc,SymbolTime" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "SymbolID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public bool Update(Chain.Model.SymbolShow model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update SymbolShow set ");
            builder.Append("SymbolTitle=@SymbolTitle,");
            builder.Append("SymbolPhoto=@SymbolPhoto,");
            builder.Append("SymbolDesc=@SymbolDesc,");
            builder.Append("SymbolTime=@SymbolTime");
            builder.Append(" where SymbolID=@SymbolID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@SymbolTitle", SqlDbType.NVarChar, 50), new SqlParameter("@SymbolPhoto", SqlDbType.NVarChar, 200), new SqlParameter("@SymbolDesc", SqlDbType.NVarChar, 0xfa0), new SqlParameter("@SymbolTime", SqlDbType.DateTime), new SqlParameter("@SymbolID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.SymbolTitle;
            cmdParms[1].Value = model.SymbolPhoto;
            cmdParms[2].Value = model.SymbolDesc;
            cmdParms[3].Value = model.SymbolTime;
            cmdParms[4].Value = model.SymbolID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

